import { Component, Injector, Inject, Optional, OpaqueToken, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import {
  API_BASE_URL,
  PagedResultDtoOfAgentOutDto,
  AgentServiceProxy,
  EntityDto
} from '@shared/service-proxies/service-proxies';

import { AppConsts } from '@shared/AppConsts';

@Component({
  selector: 'app-agent-confirmed-list',
  templateUrl: './agent-confirmed-list.component.html',
  styleUrls: ['./agent-confirmed-list.component.scss'],
  animations: [appModuleAnimation()]
})
export class AgentConfirmedListComponent extends AppComponentBase implements OnInit {

  agentList: PagedResultDtoOfAgentOutDto = new PagedResultDtoOfAgentOutDto();
  condition: GetAgentsForAdminCondition = new GetAgentsForAdminCondition();
  downloadUrl: string = AppConsts.remoteServiceBaseUrl + '/file/download/';
  currentPage: number = 1;
  constructor(
    injector: Injector,
    private _agentService: AgentServiceProxy,
    private route: ActivatedRoute
  ) {
    super(injector);
  }
  ngOnInit(): void {
    this.condition.userType = "";
    this.condition.maxResultCount = 10;
    this.getAgents();
  }

  getAgents(): void {
    this._agentService.getAgentsForAdminAsync(this.condition.maxResultCount,
      this.condition.skipCount,
      this.condition.sorting,
      this.condition.userType,
      this.condition.keyword
    ).subscribe((result: PagedResultDtoOfAgentOutDto) => {
      this.agentList = result;
    });
  }

  revoke(id: number): void {
    abp.message.confirm('确定要撤销这个店长?', '请确认', (result: boolean) => {
      if (result) {
        const input = new EntityDto();
        input.id = id;
        this._agentService.revokeAgentAsync(input).subscribe(() => {
          abp.notify.success('操作成功!');
          this.getAgents();
        });
      }
    });
  }

  pageChanged($event: any): void {
    this.condition.skipCount = this.condition.maxResultCount * $event.page;
    this.getAgents();
  }

  translateAgentType(userType: string): string {
    if (userType === 'FirstAgentUser') {
      return '队长';
    }
    if (userType === 'SecondAgentUser') {
      return '店员';
    }
  }
}


export class GetAgentsForAdminCondition {
  maxResultCount?: number;
  skipCount?: number;
  sorting?: string;
  userType?: string;
  keyword?: string;
}
